package com.ssyt.examinationsystem.controller;

import com.ssyt.examinationsystem.entity.CourseInfo;
import com.ssyt.examinationsystem.service.ICourseInfoService;
import com.ssyt.examinationsystem.service.impl.CourseInfoServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

/**
 * Servlet implementation class CourseInfoController
 */
public class CourseInfoController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public CourseInfoController() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     * response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     * response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 创建一个业务对象
        ICourseInfoService courseService = new CourseInfoServiceImpl();
        // 得到输出流对象
        PrintWriter out = response.getWriter();
        // 得到地址栏上的参数
        String flag = request.getParameter("flag");
        if ("list".equals(flag)) {
            List(courseService, request, response);
        } else if ("initAdd".equals(flag)) {
            initAdd(courseService, request, response);
        } else if ("add".equals(flag)) {
            add(courseService, request, response, out);
        } else if ("delete".equals(flag)) {
            dalete(courseService, request, response, out);
        } else if ("initUpdate".equals(flag)) {
            initUpdate(courseService, request, response);
        } else if ("update".equals(flag)) {
            update(courseService, request, response, out);
        }

        out.flush();
        out.close();
    }

    /**
     * 修改科目的方法
     *
     * @param courseService
     * @param request
     * @param response
     * @param out
     * @throws UnsupportedEncodingException
     */
    private void update(ICourseInfoService courseService, HttpServletRequest request, HttpServletResponse response,
                        PrintWriter out) throws UnsupportedEncodingException {
        // 创建一个角色对象，并封装在修改页面传过来的表单数据
        CourseInfo course = new CourseInfo();
        Integer courseId = Integer.valueOf(request.getParameter("courseId"));
        course.setCourseId(courseId);

        String courseName = request.getParameter("courseName");
        courseName = URLDecoder.decode(courseName, "UTF-8");
        course.setCourseName(courseName);
        // 调用修改的业务方法
        int row = courseService.updateCourseInfo(course);
        // 判断是否修改成功
        if (row > 0) {
            out.print("ok");
        }
    }

    /**
     * 初始化修改的方法
     *
     * @param courseService
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    private void initUpdate(ICourseInfoService courseService, HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 获取当前修改角色的编号
        String courseId = request.getParameter("courseId");
        Map<String, Object> course = courseService.getCourseInfoByRoleId(courseId);
        request.setAttribute("course", course);

        request.getRequestDispatcher("WEB-INF/views/course/courseUpdate.jsp").forward(request, response);
    }

    /**
     * 删除科目的方法
     *
     * @param courseService
     * @param request
     * @param response
     * @param out
     */
    private void dalete(ICourseInfoService courseService, HttpServletRequest request, HttpServletResponse response,
                        PrintWriter out) {
        // 获取删除角色的编号
        String courseId = request.getParameter("courseId");
        // 调用删除角色信息的业务方法
        int row = courseService.deleteCourseInfo(courseId);
        if (row > 0) {
            out.print("ok");
        }
    }

    /**
     * 添加科目的方法
     *
     * @param courseService
     * @param request
     * @param response
     * @param out
     * @throws UnsupportedEncodingException
     */
    private void add(ICourseInfoService courseService, HttpServletRequest request, HttpServletResponse response,
                     PrintWriter out) throws UnsupportedEncodingException {
        // 获取添加的表单数据
        String courseName = request.getParameter("courseName");
        // 对网页数据进行转码
        courseName = URLDecoder.decode(courseName, "UTF-8");
        CourseInfo course = new CourseInfo();
        course.setCourseName(courseName);
        // 调用添加科目信息的业务方法
        int row = courseService.addCourseInfo(course);
        if (row > 0) {
            out.print("ok");
        }
    }

    /**
     * 初始化添加科目的方法
     *
     * @param courseService
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void initAdd(ICourseInfoService courseService, HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.getRequestDispatcher("WEB-INF/views/course/courseAdd.jsp").forward(request, response);
    }

    /**
     * 查询所有的科目的方法
     *
     * @param courseService
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    private void List(ICourseInfoService courseService, HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 调用查询所有的业务方法
        List<CourseInfo> courseList = courseService.getCourseInfos();
        // 将集合存放到请求对象中
        request.setAttribute("courseList", courseList);
        // 转发到WEB-INF/views/course/courseList.jsp页面
        request.getRequestDispatcher("WEB-INF/views/course/courseList.jsp").forward(request, response);
    }

}
